import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axioscy from "axios";
//配置axios的全局基本路径
axioscy.defaults.baseURL='http://localhost:8081/'
// axioscy.defaults.baseURL='/api'
Vue.prototype.$http=axioscy;
// import 'element-ui/lib/theme-default/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => { //路由拦截器
  //NProgress.start();
  if (to.path == '/login') {
    localStorage.removeItem('loginInfo');
  }
  let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
  if (!loginInfo && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})

//1 使用axios前置拦截器，让所有的请求都携带token
axioscy.interceptors.request.use(config=>{
  //携带token
  let token =  localStorage.getItem("token");
  if(token){
    config.headers['token']=token;
  }
  return config;
},error => {
  Promise.reject(error);
})

//2 使用axios后置拦截器，处理没有登录请求
axioscy.interceptors.response.use(result=>{
  console.log(result.data+"jjjjjjj");
  let data = result.data;  // {success:false,message:"noLogin"}
  // 说明后端返回错误信息是 未登录，那么就要跳转到登录页面
  if(!data.success && data.message==="noLogin"){
    router.push({ path: '/login' });
  }
  return result;
},error => {
  Promise.reject(error);
})

router.afterEach(transition => {
NProgress.done();
});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

